package com.example.financeapi.controller;

import com.example.financeapi.config.annotation.PassToken;
import com.example.financeapi.entity.FUser;
import com.example.financeapi.entity.vo.ResultVOUtil;
import com.example.financeapi.entity.vo.ResultVo;
import com.example.financeapi.utils.DateTimeUtils;
import com.example.financeapi.utils.JsonUtils;
import com.example.financeapi.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/redis")
public class RedisController {

	@Autowired
	private StringRedisTemplate strRedis;
	
	@Autowired
	private RedisUtils redis;

	/**
	 * https://mp.weixin.qq.com/s/i9dkHQYjzOu0BNx3_DSBVg
	 * Spring Boot 接口幂等插件使用
	 * @return
	 */
	@PassToken
	//@Idempotent(key = "#key", expireTime = 10, info = "请勿重复查询")
	@RequestMapping("/test")
	public ResultVo test(){
		redis.set("zly-cache", "Hello Redis ~");
		String result =redis.get("zly-cache");
		return ResultVOUtil.success(result);
	}

	@PassToken
	@RequestMapping("/getObj")
	public ResultVo getObj(){

		FUser user = new FUser();
		user.setId(1100001);
		user.setMiniappOpenid("test001_" + DateTimeUtils.getCurrentTimeStr());
		user.setMpOpenid("test001_" + DateTimeUtils.getCurrentTimeStr());
		user.setUnionid("test001_" + DateTimeUtils.getCurrentTimeStr());
		user.setUpdateTime(new Date());
		user.setCreateTime(DateTimeUtils.getCurrentDateTimeStr());
		strRedis.opsForValue().set("json:user", JsonUtils.objectToJson(user));
		FUser jsonUser =JsonUtils.jsonToPojo(strRedis.opsForValue().get("json:user"), FUser.class);
		return ResultVOUtil.success(jsonUser);
	}

	@PassToken
	@RequestMapping("/getJsonList")
	public ResultVo getJsonList(){
		FUser user = new FUser();
		user.setId(1100001);
		user.setMiniappOpenid("test001_" + DateTimeUtils.getCurrentTimeStr());
		user.setMpOpenid("test001_" + DateTimeUtils.getCurrentTimeStr());
		user.setUnionid("test001_" + DateTimeUtils.getCurrentTimeStr());
		user.setUpdateTime(new Date());
		user.setCreateTime(DateTimeUtils.getCurrentDateTimeStr());

		FUser user1 = new FUser();
		user1.setId(1100002);
		user1.setMiniappOpenid("test002_" + DateTimeUtils.getCurrentTimeStr());
		user1.setMpOpenid("test002_" + DateTimeUtils.getCurrentTimeStr());
		user1.setUnionid("test002_" + DateTimeUtils.getCurrentTimeStr());
		user1.setUpdateTime(new Date());
		user1.setCreateTime(DateTimeUtils.getCurrentDateTimeStr());

		FUser user2 = new FUser();
		user2.setId(1100003);
		user2.setMiniappOpenid("test003_" + DateTimeUtils.getCurrentTimeStr());
		user2.setMpOpenid("test003_" + DateTimeUtils.getCurrentTimeStr());
		user2.setUnionid("test003_" + DateTimeUtils.getCurrentTimeStr());
		user2.setUpdateTime(new Date());
		user2.setCreateTime(DateTimeUtils.getCurrentDateTimeStr());
		
		List<FUser> userList = new ArrayList<FUser>();
		userList.add(user);
		userList.add(user1);
		userList.add(user2);
		
		redis.set("json:info:userList", JsonUtils.objectToJson(userList),2000);
		String userListJson= redis.get("json:info:userList");
		List<FUser> userListResult = JsonUtils.jsonToList(userListJson, FUser.class);
		return ResultVOUtil.success(userListResult);
	}

}
